
import Tools from "@/CTManage/utils/Tools";
import DateUtils from "@/CTManage/utils/DateUtils";

const space = '  ';

const LogUtils = {

  log(obj, na, preContext) {
    this.debug(obj, na, preContext);
  },

  /*
    打印对象里面所有内容
  */
  debug(obj, na, preContext) {

    if(Tools.isNull(na)) {
      na = 'obj';
    }

    if(na == '_this') {
      console.log(preContext + na);
      return;
    }

    if(Tools.isNull(preContext)) {
      preContext = '';
    }

    if(Tools.isNull(obj)) {
      console.log(preContext + na + ' 为空');
      return;
    }

    let preContext2 = preContext + space;

    if(obj instanceof Function) { /* 方法只打印名称 */

      /* console.log('pre function'); */
      console.log(preContext + na + 'is a function');
    } else if(obj instanceof Array) { // 打印数组

      /* console.log('pre .length'); */
      console.log(preContext + na + '.length= ' + obj.length);
      for(let index = 0; index < obj.length; index++) {
        if(typeof obj[index] == "object") {

          this.debug(obj[index], na + '[' + String(index) + ']', preContext2);
        } else {

         /*  console.log('pre arr na='); */
          console.log(preContext2 + na + '[' + String(index) + ']= ' + obj[index]);
        }
      }
    } else if(obj instanceof Date) { /* 打印日期类型 */

      console.log(preContext + na + '= ' + DateUtils.getDateTime(obj));
    } else if(obj instanceof Object) { /* 循环打印对象属性值 */

      /* console.log('pre Object name='); */
      console.log(preContext + na + ': ');
      for(name in obj) {
        this.debug(obj[name], name, preContext2);
      }
    } else { /* 打印基本数据类型 */

      /* console.log('pre name='); */
      console.log(preContext + na + '= ' + obj);
    }
  },

  error(obj, na, preContext) {
    this.debug(obj, na, preContext);
  }
}

export default LogUtils;
